package org.speed.jdk;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;


public class Test {

	
	public static int aMethod(int i)throws Exception
    {
        try{
            return i / 10;
        }
        catch (Exception ex)
        {
            throw new Exception("exception in a Method");
        } finally{
            System.out.printf("finally");
        }
    }
	
	
	
	
	
	public static class Data {
		
		private int a = 0 ;
		
		private ReentrantLock lock =  new ReentrantLock();
		private Condition even = lock.newCondition();
		private Condition old = lock.newCondition();
		
		private Condition condition = lock.newCondition();

		
		public void incrAndPrint() {
			lock.lock();
			
			
			a++;
			
			try {
				if(a % 2 == 0 ) {
					System.out.println("偶数:" + a);
					
					
					old.signal();
					try {
						even.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					/**
					try {
						condition.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}**/
					
				}else {
					System.out.println("奇数:" + a);
					
					
					even.signal();
					try {
						old.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					/**
					condition.signal();**/
					
				}
				
			}finally {
				
				lock.unlock();
			}
		}
		
	}
	
	
	void sort() {
		
		
	}
	
	
	
	
	
	
	public static void main(String [] args)
    {
		
		/**
		Data data = new Data();
		Runnable task = new Runnable() {
			@Override
			public void run() {
				int i = 0 ;
				while(i++ < 50) {
					data.incrAndPrint();
				}
			}
		};
		 
		new Thread(task).start();
		new Thread(task).start();**/
		
		
		
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
	             new ThreadFactory() {
					private AtomicInteger atomicInteger = new AtomicInteger(0);
					
					@Override
					public Thread newThread(Runnable r) {
						Thread thread = new Thread(r);
						thread.setName("Thread-Name-" + atomicInteger.incrementAndGet());
						
						/**
						thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
							@Override
							public void uncaughtException(Thread t, Throwable e) {
								System.out.println(t.getName() );
								//e.printStackTrace();
							}
						});**/
						
						
						return thread;
					}
			
		}, new RejectedExecutionHandler() {
			
			@Override
			public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
				
				System.out.println("rejectedExecution");
				
			}
		});
		
		
		
		
		
		threadPoolExecutor.execute(new Runnable() {
			
			@Override
			public void run() {
				int i = 0 ;
				while (i++ < 10) {
					System.out.println("==");
					
					try {
						Thread.currentThread().sleep(1000 * 1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
				}
				
			}
		});
		
		
		threadPoolExecutor.execute(new Runnable() {
			
			@Override
			public void run() {
				System.out.println("errrr");
				System.out.println(1/0);
			}
		});
		
		System.out.println("okkkk");
		
		
		
		
		
    }
	
	
	
	
	
}
